/*
 * @Author: 0x9DEFA478
 * @Date: 2022-01-22 23:05:05
 * @LastEditTime: 2022-02-23 16:40:59
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#ifndef __Console_H_NES_APU_H_
#define __Console_H_NES_APU_H_
#include "../H_NES.h"




#define vH_NES_APU_FrameCounter_Reg_4017                          0xFF000000U
#define vH_NES_APU_FrameCounter_QuarterFrame                      0x00800000U
#define vH_NES_APU_FrameCounter_HalfFrame                         0x00400000U
#define vH_NES_APU_FrameCounter_CPUClock                          0x0000FFFFU




typedef struct{

  struct{
    Huint32 Reg_4017_CPUClock;
  }FrameCounter;
  
  struct{

    struct{

      struct{
        Huint32 Period_Cnt;//bit[0:15]:Cnt bit[16:31]:Period
      }Timer;

      Hbyte Reg_4000;

      struct{
        Huint16 TargetTimerPeriod;
        Huint16 Reg_4001_PCnt;//bit[0:2]:PCnt bit[3]:reload flag bit[4]:mute bit[8:15]:$4001
      }Sweep;

      struct{
        Hbyte DividerValue;
        Hbyte StartFlag;
        Hbyte DecayLevel;
      }Envelope;

      struct{
        Hbyte Step;
      }Sequencer;

      struct{
        Huint16 Period_Cnt;//bit[0:7]:Cnt bit[8:15]:Period
      }LengthCounter;

    }Pulse1;

    struct{

      struct{
        Huint32 Period_Cnt;//bit[0:15]:Cnt bit[16:31]:Period
      }Timer;

      Hbyte Reg_4004;

      struct{
        Huint16 TargetTimerPeriod;
        Huint16 Reg_4005_PCnt;//bit[0:2]:PCnt bit[3]:reload flag bit[4]:mute bit[8:15]:$4005
      }Sweep;

      struct{
        Hbyte DividerValue;
        Hbyte StartFlag;
        Hbyte DecayLevel;
      }Envelope;

      struct{
        Hbyte Step;
      }Sequencer;

      struct{
        Huint16 Period_Cnt;//bit[0:7]:Cnt bit[8:15]:Period
      }LengthCounter;

    }Pulse2;

    struct{

      struct{
        Huint32 Period_Cnt;//bit[0:15]:Cnt bit[16:31]:Period
      }Timer;

      struct{
        Huint16 Reg_4008_Cnt;//bit[8:15]:$4008 bit[7]:reload flag bit[0:6]:Cnt
      }LinerCounter;
      struct{
        Huint16 Period_Cnt;//bit[0:7]:Cnt bit[8:15]:Period
      }LengthCounter;

      Hbyte Output;//bit[0:3]:Value Bit[7]:IsDec

    }Triangle;

    struct{

      Huint16* TimerPeriodTable;

      struct{
        Huint32 Period_Cnt;//bit[0:15]:Cnt bit[16:31]:Period
      }Timer;

      Hbyte Reg_400C;
      Hbyte ModeFlag;

      struct{
        Hbyte DividerValue;
        Hbyte StartFlag;
        Hbyte DecayLevel;
      }Envelope;

      Huint16 ShiftRegister;//上电时为1

      struct{
        Huint16 Period_Cnt;//bit[0:7]:Cnt bit[8:15]:Period
      }LengthCounter;

    }Noise;

    struct{

      Huint16* TimerPeriodTable;
      
      struct{
        Huint32 Period_Cnt;//bit[0:15]:Cnt bit[16:31]:Period
      }Timer;

      Hbyte Reg_4010;

      struct{
        Huint16 Address;
        Huint16 NumOfLastByte;

        Huint16 Period_Address;
        Huint16 Period_NumOfLastByte;
      }Reader;

      struct{
        Hbyte NumOfLastBit;
        Hbyte Value;
      }Shifter;

      Hbyte MultFlag;
      Hbyte OutputLevel;

    }DMC;
    
  }Channel;

  struct{
    Hbyte Reg_4015_W;
  }Status;

  struct{

#if vH_NES_APU_OutputChannelSrc == 0
    struct{
      Hint32 a;
      Hint32 last;
    }LPF;
    int DivCnt;
#endif

    int OutputIndex;
#if vH_NES_APU_Enable != 0
    void* Buffer;
#endif
  }Output;
  
}H_NES_APU;




#endif //__Console_H_NES_APU_H_
